Skip to content

support for image attachments when classifying questions #6675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kitsune/llm/questions/classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def classify_question(question: "Question") -> dict[str, Any]:
payload: dict[str, Any] = {
"subject": question.title,
"question": question.content,
"image_urls": [image.get_absolute_url() for image in question.get_images()],
"product": product,
"topics": get_taxonomy(
product, include_metadata=["description", "examples"], output_format="JSON"
Expand Down
44 changes: 39 additions & 5 deletions kitsune/llm/questions/prompt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
from langchain.prompts import ChatPromptTemplate
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.schema import HumanMessage
from langchain.schema.runnable import RunnableLambda

SPAM_INSTRUCTIONS = """
# Role and goal
Expand Down Expand Up @@ -120,17 +122,49 @@
)


spam_prompt = ChatPromptTemplate(
spam_prompt_template = ChatPromptTemplate(
(
("system", SPAM_INSTRUCTIONS),
("human", USER_QUESTION),
MessagesPlaceholder("human_message"),
)
).partial(format_instructions=spam_parser.get_format_instructions())


topic_prompt = ChatPromptTemplate(
topic_prompt_template = ChatPromptTemplate(
(
("system", TOPIC_INSTRUCTIONS),
("human", USER_QUESTION),
MessagesPlaceholder("human_message"),
)
).partial(format_instructions=topic_parser.get_format_instructions())


def create_human_message(payload: dict) -> dict:
"""
Creates the human message, with the image URL's if they're present, and
then adds it to the payload dict. Returns the modified payload dict.
"""
content: list[dict] = [
{
"type": "text",
"text": USER_QUESTION.format(**payload),
},
]

for image_url in payload.get("image_urls", ()):
content.append(
{
"type": "image_url",
"image_url": {
"url": image_url,
},
}
)

payload["human_message"] = [HumanMessage(content=content)]
return payload


spam_prompt = RunnableLambda(create_human_message) | spam_prompt_template


topic_prompt = RunnableLambda(create_human_message) | topic_prompt_template